package com.zj.fx.controller.month;

import com.github.pagehelper.PageInfo;
import com.zj.fx.common.base.BaseController;
import com.zj.fx.common.domain.AjaxResult;
import com.zj.fx.common.domain.ResultTable;
import com.zj.fx.model.custom.Tablepar;
import com.zj.fx.model.info.InfoProject;
import com.zj.fx.model.info.InfoSupply;
import com.zj.fx.model.month.*;
import com.zj.fx.service.info.InfoProjectService;
import com.zj.fx.service.info.InfoSupplyService;
import com.zj.fx.service.month.*;
import com.zj.fx.shiro.util.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 月度供电所审核任务填报明细Controller
 *
 * @author SQL
 * @ClassName: MonthSupplytaskController
 * @date 2023-06-09 17:15:40
 */
@Api(value = "月度供电所审核任务填报明细")
@Controller
@RequestMapping("/MonthSupplytaskAuditController")
public class MonthSupplytaskAuditController extends BaseController {

    private String prefix = "month/monthSupplytaskAudit";

    @Autowired
    private MonthSupplytaskAuditService monthSupplytaskAuditService;

    @Autowired
    private InfoProjectService infoProjectService;

    @Autowired
    private MonthNecessaryService monthNecessaryService;

    @Autowired
    private MonthSupplytaskDtlService monthSupplytaskDtlService;

    @Autowired
    private MonthSupplytaskService monthSupplytaskService;

    @Autowired
    private MonthResultService monthResultService;

    @Autowired
    private MonthResultDtlService monthResultDtlService;

    @Autowired
    private MonthAuditDtlService monthAuditDtlService;

    @Autowired
    private InfoSupplyService infoSupplyService;

    @Autowired
    private MonthAuditSubService monthAuditSubService;


    /**
     * 月度供电所审核任务填报明细页面展示
     *
     * @param model
     * @return String
     * @author SQL
     */
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/view")
    @RequiresPermissions("gen:monthSupplytaskAudit:view")
    public String view(ModelMap model) {
        return prefix + "/list";
    }

    /**
     * list集合
     *
     * @param tablepar
     * @param
     * @return
     */
    //@Log(title = "月度供电所审核任务填报明细", action = "111")
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/list")
    @RequiresPermissions("gen:monthSupplytaskAudit:list")
    @ResponseBody
    public ResultTable list(Tablepar tablepar, MonthSupplytask monthSupplytask) {
        //查询完成全部月度审核任务的分公司
        //-----查询当前月份下的评价任务中的不同分公司id及名称
//        List<MonthSupplytask> monthSupplytasks = monthSupplytaskService.getDistinctSubbyMonth(monthSupplytask);

        PageInfo<MonthSupplytask> page = monthSupplytaskAuditService.list4self(tablepar, monthSupplytask);
        return pageTable(page.getList(), page.getTotal());
    }

    /**
     * 新增跳转
     */
    @ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        return prefix + "/add";
    }

    /**
     * 新增保存
     *
     * @param
     * @return
     */
    //@Log(title = "月度供电所审核任务填报明细新增", action = "111")
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/add")
    @RequiresPermissions("gen:monthSupplytaskAudit:add")
    @ResponseBody
    public AjaxResult add(MonthSupplytask MonthSupplytask) {
        int b = monthSupplytaskAuditService.insertSelective(MonthSupplytask);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 月度供电所审核任务填报明细删除
     *
     * @param ids
     * @return
     */
    //@Log(title = "月度供电所审核任务填报明细删除", action = "111")
    @ApiOperation(value = "删除", notes = "删除")
    @DeleteMapping("/remove")
    @RequiresPermissions("gen:monthSupplytaskAudit:remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        int b = monthSupplytaskAuditService.deleteByPrimaryKey(ids);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }


    /**
     * 修改跳转
     *
     * @param id
     * @param
     * @return
     */
    @ApiOperation(value = "修改跳转", notes = "修改跳转")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map) {
        map.put("MonthSupplytask", monthSupplytaskAuditService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    //@Log(title = "月度供电所审核任务填报明细修改", action = "111")
    @ApiOperation(value = "修改保存", notes = "修改保存")
    @RequiresPermissions("gen:monthSupplytaskAudit:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MonthSupplytask MonthSupplytask) {
        return toAjax(monthSupplytaskAuditService.updateByPrimaryKeySelective(MonthSupplytask));
    }

    /**
     * 审核页面跳转
     */
    @ApiOperation(value = "审核页面跳转", notes = "审核页面跳转")
    @GetMapping("/audit/{id}")
    public String fill(@PathVariable("id") String id, ModelMap modelMap) {
        List<InfoProject> resultList = new ArrayList<>();
        //查询必备项目标签
        List<MonthNecessary> monthNecessaryList = monthNecessaryService.getTabExist(id);
        if (monthNecessaryList != null && monthNecessaryList.size() > 0) {
            InfoProject infoProject = new InfoProject();
            infoProject.setId("0");
            infoProject.setNum("0");
            infoProject.setContent("必备条件");
            infoProject.setType("1");
            resultList.add(infoProject);
        }
        //查询动态标签
        List<InfoProject> infoProjects = infoProjectService.getAuditTabbyDepandTaskID(id);
        resultList.addAll(infoProjects);
        modelMap.put("tabTitleList", resultList);
        modelMap.put("taskId", id);
        return prefix + "/audit";
    }

    /**
     * 更新月度填报项目及四级项目分数计算
     *
     * @param
     * @param
     * @return
     */
    @ApiOperation(value = "更新月度必备项目自评价结果", notes = "更新月度必备项目自评价结果")
    @PostMapping("/updateAudit")
    @ResponseBody
    public AjaxResult updateAudit(@RequestBody MonthSupplytaskDtl monthSupplytaskDtl) {
        //判断审核值与自评价值是否相同，相同的给出提示不做更新
        MonthSupplytaskDtl monthSupplytaskDtl1 = monthSupplytaskDtlService.selectByPrimaryKey(monthSupplytaskDtl.getId());
        if(!MonthConstant.DTL_TACTIC_ISORNOT.equals(monthSupplytaskDtl1.getTactics())){
            if (Strings.isBlank(monthSupplytaskDtl1.getReviewValue())){
                if (monthSupplytaskDtl1.getSelfValue().equals(monthSupplytaskDtl.getReviewValue())){
                    return AjaxResult.error("审核更正结果与自评价值相同,无需审核更正");
                }
            }else {

            }
        }else {
            if (Strings.isBlank(monthSupplytaskDtl1.getReviewValue())){
                if (monthSupplytaskDtl1.getSelfValue().equals(monthSupplytaskDtl.getReviewValue())){
                    return AjaxResult.error("审核更正结果与自评价值相同,无需审核更正");
                }
            }
        }

        MonthSupplytaskDtl monthSupplytaskDtl4 = monthSupplytaskAuditService.updateAudit(monthSupplytaskDtl);
        return toAjax(monthSupplytaskDtlService.updateByPrimaryKeySelective(monthSupplytaskDtl4));
    }

    /**
     * 责任部门审核提报--页面内单个提报
     *
     * @param
     * @param
     * @return
     */
    @ApiOperation(value = "审核提报", notes = "审核提报")
    @PostMapping("/auditCommit")
    @ResponseBody
    @Transactional
    public AjaxResult auditCommit(@RequestBody MonthSupplytask monthSupplytask) {
        //0、关键数据获取及数据初始化
        String id = monthSupplytask.getId();

        String typeOk = "1";

        //一、校验
        //1.1 所有必备项目是否进行审核评价，未评价禁止提交
//        List<MonthNecessary> monthNecessaries = monthNecessaryService.findNoAudit(monthSupplytask);
//        if (monthNecessaries!=null && monthNecessaries.size()!=0){
//            return AjaxResult.error("项目编号为:"+monthNecessaries.get(0).getNum()+"的必备项目审核评价未填写，请填写后提交");
//        }
        //1.2 所有五级任务是否有驳回状态下的任务，有则禁止提交
        monthSupplytask.setTaskState(MonthConstant.TASK_STATE_REJECTDEPT);
        List<MonthSupplytaskDtl> monthSupplytaskDtls = monthSupplytaskDtlService.findbyDepandTaskidandState(monthSupplytask);
        if (monthSupplytaskDtls!=null && monthSupplytaskDtls.size()>0){
            String num = monthSupplytaskDtls.get(0).getNum();
            return AjaxResult.error("指标编号为:"+num+"行数据处于驳回处理状态，请等待驳回调整后复核完成再进行提交");
        }
        //1.3 当前部门下对应的所有五级任务是否有驳回状态下的任务，有则禁止提交
        monthSupplytask.setTaskState(MonthConstant.DTL_STATE_REJECTDEPT);
        List<MonthSupplytaskDtl> monthSupplytaskDtls1 = monthSupplytaskDtlService.findbyDepandTaskidandState(monthSupplytask);
        if (monthSupplytaskDtls1!=null && monthSupplytaskDtls1.size()>0){
            String num = monthSupplytaskDtls1.get(0).getNum();
            return AjaxResult.error("指标编号为:"+num+"行数据处于驳回处理后未进行复审，请复审后进行提交");
        }

        //二、将部门下对应必备项目、四级、5级任务状态改为”审核完成“，更新审核完成时间
        //2.1 必备
        MonthNecessaryExample monthNecessaryExample1 = new MonthNecessaryExample();
        MonthNecessaryExample.Criteria criteria = monthNecessaryExample1.createCriteria();
        criteria.andFkTaskIdEqualTo(id);
        criteria.andResDeptIdEqualTo(ShiroUtils.getDepId());
        List<MonthNecessary> monthNecessaries1 = monthNecessaryService.selectByExample(monthNecessaryExample1);
        for (MonthNecessary monthNecessary : monthNecessaries1) {
            //状态置为审核完成
            monthNecessary.setTaskState(MonthConstant.DTL_STATE_ISFINISHED);
            if (!MonthConstant.IS.equals(monthNecessary.getIsAuditedit())){
                monthNecessary.setTypeReview(monthNecessary.getTypeSelf());
                monthNecessary.setIsAuditedit(MonthConstant.NOT);
            }
            monthNecessaryService.updateByPrimaryKeySelective(monthNecessary);
        }
        //2.2 明细表四级、五级
        //四级明细---查询部门负责的对应四级项目明细编号
        MonthSupplytaskDtl monthSupplytaskDtl = new MonthSupplytaskDtl();
        monthSupplytaskDtl.setFkTaskId(id);
        monthSupplytaskDtl.setDeptId(ShiroUtils.getDepId());
        List<String> monthSupplytaskDtlsList = monthSupplytaskDtlService.findDistinctNum4byDep(monthSupplytaskDtl);
        for (String num : monthSupplytaskDtlsList) {
            //根据四级项目编号查询对应四级明细项目--状态置为审核完成
            MonthSupplytaskDtl monthSupplytaskDtl4 = new MonthSupplytaskDtl();
            monthSupplytaskDtl4.setFkTaskId(id);
            monthSupplytaskDtl4.setNum(num);
            monthSupplytaskDtl4.setProGrade(MonthConstant.DTL_PROGRADE_FOUR);
            List<MonthSupplytaskDtl> monthSupplytaskDtlsList4 = monthSupplytaskDtlService.findbyTaskIdandNum(monthSupplytaskDtl4);
            if (monthSupplytaskDtlsList4 != null && monthSupplytaskDtlsList4.size()==1){
                MonthSupplytaskDtl monthSupplytaskDtl1 = monthSupplytaskDtlsList4.get(0);
                if (MonthConstant.NOT.equals(monthSupplytaskDtl1.getIsAuditedit())){
                    monthSupplytaskDtl1.setReviewScore(monthSupplytaskDtl1.getSelfScore());
                }
                monthSupplytaskDtl1.setTaskState(MonthConstant.DTL_STATE_ISFINISHED);
                monthSupplytaskDtlService.updateByPrimaryKeySelective(monthSupplytaskDtl1);
            }else {
                return AjaxResult.error("指标编号为:"+num+"行数据提交时数据异常，请联系管理员");
            }
            //根据四级项目编号查询对应下属五级明细项目
            monthSupplytaskDtl.setFkParentProjectId(num);
            monthSupplytaskDtl.setProGrade(MonthConstant.DTL_PROGRADE_FIVE);
            List<MonthSupplytaskDtl> monthSupplytaskDtls5 = monthSupplytaskDtlService.findbyDepandTaskidandGrade(monthSupplytaskDtl);
            for (MonthSupplytaskDtl supplytaskDtl : monthSupplytaskDtls5) {
                if (MonthConstant.NOT.equals(supplytaskDtl.getIsAuditedit())){
                    supplytaskDtl.setReviewScore(supplytaskDtl.getSelfScore());
                    supplytaskDtl.setReviewValue(supplytaskDtl.getSelfValue());
                }
                supplytaskDtl.setTaskState(MonthConstant.DTL_STATE_ISFINISHED);
                monthSupplytaskDtlService.updateByPrimaryKeySelective(supplytaskDtl);
            }
        }

        //2.3 月度部门审核明细中的数据进行状态更新，将当前部门对应的当前任务状态改为“审核完成”
        MonthAuditDtl monthAuditDtl = new MonthAuditDtl();
        monthAuditDtl.setFkTaskId(id);
        List<MonthAuditDtl> monthAuditDtls = monthAuditDtlService.findbyTaskIDandDepID(monthAuditDtl);
        if (monthAuditDtls!=null && monthAuditDtls.size()>0){
            MonthAuditDtl monthAuditDtl1 = monthAuditDtls.get(0);
            monthAuditDtl1.setTaskState(MonthConstant.AUDIT_FINISH);
            monthAuditDtl1.setAuditDate(new Date());
            monthAuditDtlService.updateByPrimaryKeySelective(monthAuditDtl1);
        }

        //2.4 四级
        //当任务下所有五级项目状态均为审核完成后，该月度任务状态置为“审核完成”
        MonthAuditDtlExample monthAuditDtlExample = new MonthAuditDtlExample();
        MonthAuditDtlExample.Criteria criteria1 = monthAuditDtlExample.createCriteria();
        criteria1.andFkTaskIdEqualTo(id);
        criteria1.andTaskStateEqualTo(MonthConstant.AUDIT_NOT);
        List<MonthAuditDtl> monthAuditDtls1 = monthAuditDtlService.selectByExample(monthAuditDtlExample);
        if (monthAuditDtls1.size()==0){
            monthSupplytask = monthSupplytaskService.selectByPrimaryKey(id);
            monthSupplytask.setTaskState(MonthConstant.DTL_STATE_ISFINISHED);
            monthSupplytask.setAuditDate(new Date());
            monthSupplytaskService.updateByPrimaryKeySelective(monthSupplytask);
        }

        //三、检查成绩表中是否已生成数据，有则更新，无则创建
        //------------查询当前任务下的所有必备项目、五级项目是否状态均为审核完成
        //------------必备
        String neccessaryState = "";
        MonthNecessaryExample monthNecessaryExample2 = new MonthNecessaryExample();
        MonthNecessaryExample.Criteria criteria2 = monthNecessaryExample2.createCriteria();
        criteria2.andFkTaskIdEqualTo(id);
        criteria2.andTaskStateNotEqualTo(MonthConstant.DTL_STATE_ISFINISHED);
        List<MonthNecessary> monthNecessaries2 = monthNecessaryService.selectByExample(monthNecessaryExample2);
        if (monthNecessaries2.size()>0){
            neccessaryState = MonthConstant.RESULT_ISAUDITING;
        }else {
            MonthNecessaryExample monthNecessaryExample3 = new MonthNecessaryExample();
            MonthNecessaryExample.Criteria criteria3 = monthNecessaryExample3.createCriteria();
            criteria3.andFkTaskIdEqualTo(id);
            criteria3.andTypeReviewEqualTo(MonthConstant.NOT_INT);
            List<MonthNecessary> monthNecessaries3 = monthNecessaryService.selectByExample(monthNecessaryExample3);
            if (monthNecessaries3.size()>0){
                neccessaryState = MonthConstant.RESULT_FAIL;
            }else {
                neccessaryState = MonthConstant.RESULT_OK;
            }
        }
        //-----------五级
        MonthSupplytaskDtlExample monthSupplytaskDtlExample = new MonthSupplytaskDtlExample();
        MonthSupplytaskDtlExample.Criteria criteria3 = monthSupplytaskDtlExample.createCriteria();
        criteria3.andFkTaskIdEqualTo(id);
        criteria3.andProGradeEqualTo(MonthConstant.DTL_PROGRADE_FIVE);
        criteria3.andTaskStateNotEqualTo(MonthConstant.DTL_STATE_ISFINISHED);
        List<MonthSupplytaskDtl> monthSupplytaskDtlList1 = monthSupplytaskDtlService.selectByExample(monthSupplytaskDtlExample);

        String resultState = "";

        if (monthNecessaries2.size()>0 || monthSupplytaskDtlList1.size()>0){
            resultState = MonthConstant.RESULT_ISAUDITING;
        }else {
            resultState = MonthConstant.RESULT_FAIL;
        }


        List<MonthResult> existbyTaskId = monthResultService.findExistbyTaskId(id);
        if (existbyTaskId!=null && existbyTaskId.size()>0){
            MonthResult monthResult = existbyTaskId.get(0);

            //查询结果主表记录，更新必备项目是否合格及结果状态
            monthResult.setIsNeccessary(neccessaryState);
            monthResult.setIsIssue(resultState);
            monthResultService.updateByPrimaryKeySelective(monthResult);

            //根据供电所id查询供电所详细信息   --供电所id与num一致
            MonthSupplytask monthSupplytask1 = monthSupplytaskService.selectByPrimaryKey(id);
            //查询当前部门负责审批的项目对应各个页签下的分数
            MonthSupplytaskDtl monthSupplytaskDtl1 = new MonthSupplytaskDtl();
            monthSupplytaskDtl1.setFkTaskId(id);
            monthSupplytaskDtl1.setDeptId(ShiroUtils.getDepId());
            List<MonthSupplytaskDtl> monthSupplytaskDtlList = monthSupplytaskDtlService.getScoreGroupbyTabbyDep(monthSupplytaskDtl1);
            for (MonthSupplytaskDtl supplytaskDtl : monthSupplytaskDtlList) {
                monthResultDtlService.createResultDtl(monthSupplytask1, id, supplytaskDtl,monthResult.getId());
            }
        }else {
            //根据供电所id查询供电所详细信息   --供电所id与num一致
            MonthSupplytask monthSupplytask1 = monthSupplytaskService.selectByPrimaryKey(id);
            InfoSupply infoSupply = infoSupplyService.selectByPrimaryKey(monthSupplytask1.getSupplyId());

            //查询月度评价总分
            String fullMark = monthSupplytaskDtlService.findFullMarkbyFktaskid(id);

            //月度结果表新增记录
            String resultId = monthResultService.createResult(monthSupplytask1, id, infoSupply,resultState,neccessaryState,fullMark);
            //月度结果明细表新增记录
            //----------查询各页签下的得分
            MonthSupplytaskDtl monthSupplytaskDtl1 = new MonthSupplytaskDtl();
            monthSupplytaskDtl1.setFkTaskId(id);
            monthSupplytaskDtl1.setDeptId(ShiroUtils.getDepId());
            List<MonthSupplytaskDtl> monthSupplytaskDtlList = monthSupplytaskDtlService.getScoreGroupbyTabbyDep(monthSupplytaskDtl1);
            for (MonthSupplytaskDtl supplytaskDtl : monthSupplytaskDtlList) {
                monthResultDtlService.createResultDtl(monthSupplytask1, id, supplytaskDtl,resultId);
            }
        }
        return success();
    }

    /**
     * 责任部门审核---一键提报
     *
     * @param
     * @param
     * @return
     */
    @ApiOperation(value = "审核提报", notes = "审核提报")
    @PostMapping("/allCommit")
    @ResponseBody
    @Transactional
    public AjaxResult allCommit(@RequestBody MonthSupplytask monthSupplytask) {

        int totalNum = 0;
        int finishNum = 0;
        int okNum = 0;
        List<MonthAuditDtl> notauditcollect = new ArrayList<>();

        String taskMonth = monthSupplytask.getTaskMonth();

        //校验开始----START
        //校验1---年月不能为空
        if (Strings.isBlank(taskMonth)){
            return AjaxResult.error("任务年月不能为空，请选择任务年月查询出对应数据再进行一键提报");
        }

        //校验2 当前月份所有分公司均已完成分公司审核上报，才允许责任责任部门一键提报
        MonthAuditSubExample monthAuditSubExample = new MonthAuditSubExample();
        monthAuditSubExample.createCriteria().andTaskMonthEqualTo(taskMonth).andTaskStateEqualTo(MonthConstant.NOT);
        List<MonthAuditSub> monthAuditSubs = monthAuditSubService.selectByExample(monthAuditSubExample);
        if (monthAuditSubs!=null && monthAuditSubs.size()>0){
            return AjaxResult.error("当前所选择月份对应任务，存在分公司未提交上报，待全部分公司完成上报后再进行一键提报");
        }

        //校验3---审核明细表中查询该责任部门已经完成审核的list、未完成审核的list
        MonthAuditDtlExample monthAuditDtlExample = new MonthAuditDtlExample();
        monthAuditDtlExample.createCriteria().andDeptIdEqualTo(ShiroUtils.getDepId()).andTaskMonthEqualTo(taskMonth);
        List<MonthAuditDtl> monthAuditDtls2 = monthAuditDtlService.selectByExample(monthAuditDtlExample);
        if (monthAuditDtls2!=null && monthAuditDtls2.size()>0){
            List<MonthAuditDtl> finishcollect = monthAuditDtls2.stream().filter(s -> s.getTaskState().equals(MonthConstant.IS)).collect(Collectors.toList());
            if (finishcollect.size()==monthAuditDtls2.size()){
                return AjaxResult.error("当前所选择月份对应任务已经全部提报完毕，无需重复提报");
            }
            notauditcollect = monthAuditDtls2.stream().filter(s -> s.getTaskState().equals(MonthConstant.NOT)).collect(Collectors.toList());
            finishNum = finishcollect.size();
            totalNum = finishNum + notauditcollect.size();
        }else {
            return AjaxResult.error("当前任务年月,当前责任部门暂无对应审核任务，无法提报");
        }

        for (MonthAuditDtl monthAuditDtl1 : notauditcollect) {
            //0、关键数据获取及数据初始化
            String id = monthAuditDtl1.getFkTaskId();
            //------------------------------对于驳回状态的判断，目前部门没有驳回操作，暂不添加校验
//            //1.1 所有五级任务是否有驳回状态下的任务，有则禁止提交
//            monthSupplytask.setTaskState(MonthConstant.TASK_STATE_REJECTDEPT);
//            List<MonthSupplytaskDtl> monthSupplytaskDtls = monthSupplytaskDtlService.findbyDepandTaskidandState(monthSupplytask);
//            if (monthSupplytaskDtls!=null && monthSupplytaskDtls.size()>0){
//                String num = monthSupplytaskDtls.get(0).getNum();
//                return AjaxResult.error("指标编号为:"+num+"行数据处于驳回处理状态，请等待驳回调整后复核完成再进行提交");
//            }
//            //1.2 当前部门下对应的所有五级任务是否有驳回状态下的任务，有则禁止提交
//            monthSupplytask.setTaskState(MonthConstant.DTL_STATE_REJECTDEPT);
//            List<MonthSupplytaskDtl> monthSupplytaskDtls1 = monthSupplytaskDtlService.findbyDepandTaskidandState(monthSupplytask);
//            if (monthSupplytaskDtls1!=null && monthSupplytaskDtls1.size()>0){
//                String num = monthSupplytaskDtls1.get(0).getNum();
//                return AjaxResult.error("指标编号为:"+num+"行数据处于驳回处理后未进行复审，请复审后进行提交");
//            }

            //二、将部门下对应必备项目、四级、5级任务状态改为”审核完成“，更新审核完成时间
            //2.1 必备
            MonthNecessaryExample monthNecessaryExample1 = new MonthNecessaryExample();
            MonthNecessaryExample.Criteria criteria = monthNecessaryExample1.createCriteria();
            criteria.andFkTaskIdEqualTo(id);
            criteria.andResDeptIdEqualTo(ShiroUtils.getDepId());
            List<MonthNecessary> monthNecessaries1 = monthNecessaryService.selectByExample(monthNecessaryExample1);
            for (MonthNecessary monthNecessary : monthNecessaries1) {
                //状态置为审核完成
                monthNecessary.setTaskState(MonthConstant.DTL_STATE_ISFINISHED);
                if (!MonthConstant.IS.equals(monthNecessary.getIsAuditedit())){
                    monthNecessary.setTypeReview(monthNecessary.getTypeSelf());
                    monthNecessary.setIsAuditedit(MonthConstant.NOT);
                }
                monthNecessaryService.updateByPrimaryKeySelective(monthNecessary);
            }
            //2.2 明细表四级、五级
            //四级明细---查询部门负责的对应四级项目明细编号
            MonthSupplytaskDtl monthSupplytaskDtl = new MonthSupplytaskDtl();
            monthSupplytaskDtl.setFkTaskId(id);
            monthSupplytaskDtl.setDeptId(ShiroUtils.getDepId());
            List<String> monthSupplytaskDtlsList = monthSupplytaskDtlService.findDistinctNum4byDep(monthSupplytaskDtl);
            for (String num : monthSupplytaskDtlsList) {
                //根据四级项目编号查询对应四级明细项目--状态置为审核完成
                MonthSupplytaskDtl monthSupplytaskDtl4 = new MonthSupplytaskDtl();
                monthSupplytaskDtl4.setFkTaskId(id);
                monthSupplytaskDtl4.setNum(num);
                monthSupplytaskDtl4.setProGrade(MonthConstant.DTL_PROGRADE_FOUR);
                List<MonthSupplytaskDtl> monthSupplytaskDtlsList4 = monthSupplytaskDtlService.findbyTaskIdandNum(monthSupplytaskDtl4);
                if (monthSupplytaskDtlsList4 != null && monthSupplytaskDtlsList4.size()==1){
                    MonthSupplytaskDtl monthSupplytaskDtl1 = monthSupplytaskDtlsList4.get(0);
                    if (MonthConstant.NOT.equals(monthSupplytaskDtl1.getIsAuditedit())){
                        monthSupplytaskDtl1.setReviewScore(monthSupplytaskDtl1.getSelfScore());
                    }
                    monthSupplytaskDtl1.setTaskState(MonthConstant.DTL_STATE_ISFINISHED);
                    monthSupplytaskDtlService.updateByPrimaryKeySelective(monthSupplytaskDtl1);
                }else {
                    return AjaxResult.error("指标编号为:"+num+"行数据提交时数据异常，请联系管理员");
                }
                //根据四级项目编号查询对应下属五级明细项目
                monthSupplytaskDtl.setFkParentProjectId(num);
                monthSupplytaskDtl.setProGrade(MonthConstant.DTL_PROGRADE_FIVE);
                List<MonthSupplytaskDtl> monthSupplytaskDtls5 = monthSupplytaskDtlService.findbyDepandTaskidandGrade(monthSupplytaskDtl);
                for (MonthSupplytaskDtl supplytaskDtl : monthSupplytaskDtls5) {
                    if (MonthConstant.NOT.equals(supplytaskDtl.getIsAuditedit())){
                        supplytaskDtl.setReviewScore(supplytaskDtl.getSelfScore());
                        supplytaskDtl.setReviewValue(supplytaskDtl.getSelfValue());
                    }
                    supplytaskDtl.setTaskState(MonthConstant.DTL_STATE_ISFINISHED);
                    monthSupplytaskDtlService.updateByPrimaryKeySelective(supplytaskDtl);
                }
            }

            //2.3 月度部门审核明细中的数据进行状态更新，将当前部门对应的当前任务状态改为“审核完成”
            MonthAuditDtl monthAuditDtl = new MonthAuditDtl();
            monthAuditDtl.setFkTaskId(id);
            List<MonthAuditDtl> monthAuditDtls = monthAuditDtlService.findbyTaskIDandDepID(monthAuditDtl);
            if (monthAuditDtls!=null && monthAuditDtls.size()>0){
                MonthAuditDtl monthAuditDtl2 = monthAuditDtls.get(0);
                monthAuditDtl2.setTaskState(MonthConstant.AUDIT_FINISH);
                monthAuditDtl2.setAuditDate(new Date());
                monthAuditDtlService.updateByPrimaryKeySelective(monthAuditDtl2);
            }

            //2.4 四级
            //当任务下所有五级项目状态均为审核完成后，该月度任务状态置为“审核完成”
            MonthAuditDtlExample dtlExample = new MonthAuditDtlExample();
            MonthAuditDtlExample.Criteria criteria1 = dtlExample.createCriteria();
            criteria1.andFkTaskIdEqualTo(id);
            criteria1.andTaskStateEqualTo(MonthConstant.AUDIT_NOT);
            List<MonthAuditDtl> monthAuditDtls1 = monthAuditDtlService.selectByExample(dtlExample);
            if (monthAuditDtls1.size()==0){
                monthSupplytask = monthSupplytaskService.selectByPrimaryKey(id);
                monthSupplytask.setTaskState(MonthConstant.DTL_STATE_ISFINISHED);
                monthSupplytask.setAuditDate(new Date());
                monthSupplytaskService.updateByPrimaryKeySelective(monthSupplytask);
            }

            //三、检查成绩表中是否已生成数据，有则更新，无则创建
            //------------查询当前任务下的所有必备项目、五级项目是否状态均为审核完成
            //------------必备
            String neccessaryState = "";
            MonthNecessaryExample monthNecessaryExample2 = new MonthNecessaryExample();
            MonthNecessaryExample.Criteria criteria2 = monthNecessaryExample2.createCriteria();
            criteria2.andFkTaskIdEqualTo(id);
            criteria2.andTaskStateNotEqualTo(MonthConstant.DTL_STATE_ISFINISHED);
            List<MonthNecessary> monthNecessaries2 = monthNecessaryService.selectByExample(monthNecessaryExample2);
            if (monthNecessaries2.size()>0){
                neccessaryState = MonthConstant.RESULT_ISAUDITING;
            }else {
                MonthNecessaryExample monthNecessaryExample3 = new MonthNecessaryExample();
                MonthNecessaryExample.Criteria criteria3 = monthNecessaryExample3.createCriteria();
                criteria3.andFkTaskIdEqualTo(id);
                criteria3.andTypeReviewEqualTo(MonthConstant.NOT_INT);
                List<MonthNecessary> monthNecessaries3 = monthNecessaryService.selectByExample(monthNecessaryExample3);
                if (monthNecessaries3.size()>0){
                    neccessaryState = MonthConstant.RESULT_FAIL;
                }else {
                    neccessaryState = MonthConstant.RESULT_OK;
                }
            }
            //-----------五级
            MonthSupplytaskDtlExample monthSupplytaskDtlExample = new MonthSupplytaskDtlExample();
            MonthSupplytaskDtlExample.Criteria criteria3 = monthSupplytaskDtlExample.createCriteria();
            criteria3.andFkTaskIdEqualTo(id);
            criteria3.andProGradeEqualTo(MonthConstant.DTL_PROGRADE_FIVE);
            criteria3.andTaskStateNotEqualTo(MonthConstant.DTL_STATE_ISFINISHED);
            List<MonthSupplytaskDtl> monthSupplytaskDtlList1 = monthSupplytaskDtlService.selectByExample(monthSupplytaskDtlExample);

            String resultState = "";

            if (monthNecessaries2.size()>0 || monthSupplytaskDtlList1.size()>0){
                resultState = MonthConstant.RESULT_ISAUDITING;
            }else {
                resultState = MonthConstant.RESULT_FAIL;
            }


            List<MonthResult> existbyTaskId = monthResultService.findExistbyTaskId(id);
            if (existbyTaskId!=null && existbyTaskId.size()>0){
                MonthResult monthResult = existbyTaskId.get(0);

                //查询结果主表记录，更新必备项目是否合格及结果状态
                monthResult.setIsNeccessary(neccessaryState);
                monthResult.setIsIssue(resultState);
                monthResultService.updateByPrimaryKeySelective(monthResult);

                //根据供电所id查询供电所详细信息   --供电所id与num一致
                MonthSupplytask monthSupplytask1 = monthSupplytaskService.selectByPrimaryKey(id);
                //查询当前部门负责审批的项目对应各个页签下的分数
                MonthSupplytaskDtl monthSupplytaskDtl1 = new MonthSupplytaskDtl();
                monthSupplytaskDtl1.setFkTaskId(id);
                monthSupplytaskDtl1.setDeptId(ShiroUtils.getDepId());
                List<MonthSupplytaskDtl> monthSupplytaskDtlList = monthSupplytaskDtlService.getScoreGroupbyTabbyDep(monthSupplytaskDtl1);
                for (MonthSupplytaskDtl supplytaskDtl : monthSupplytaskDtlList) {
                    monthResultDtlService.createResultDtl(monthSupplytask1, id, supplytaskDtl,monthResult.getId());
                }
            }else {
                //根据供电所id查询供电所详细信息   --供电所id与num一致
                MonthSupplytask monthSupplytask1 = monthSupplytaskService.selectByPrimaryKey(id);
                InfoSupply infoSupply = infoSupplyService.selectByPrimaryKey(monthSupplytask1.getSupplyId());

                //查询月度评价总分
                String fullMark = monthSupplytaskDtlService.findFullMarkbyFktaskid(id);

                //月度结果表新增记录
                String resultId = monthResultService.createResult(monthSupplytask1, id, infoSupply,resultState,neccessaryState,fullMark);
                //月度结果明细表新增记录
                //----------查询各页签下的得分
                MonthSupplytaskDtl monthSupplytaskDtl1 = new MonthSupplytaskDtl();
                monthSupplytaskDtl1.setFkTaskId(id);
                monthSupplytaskDtl1.setDeptId(ShiroUtils.getDepId());
                List<MonthSupplytaskDtl> monthSupplytaskDtlList = monthSupplytaskDtlService.getScoreGroupbyTabbyDep(monthSupplytaskDtl1);
                for (MonthSupplytaskDtl supplytaskDtl : monthSupplytaskDtlList) {
                    monthResultDtlService.createResultDtl(monthSupplytask1, id, supplytaskDtl,resultId);
                }
            }
        }
        return success();
    }

    /**
     * 查看提报结果跳转
     */
    @ApiOperation(value = "查看提报结果跳转", notes = "查看提报结果跳转")
    @GetMapping("/viewResult/{id}")
    public String viewResult(@PathVariable("id") String id, ModelMap modelMap) {
        List<InfoProject> resultList = new ArrayList<>();
        //查询必备项目标签
        List<MonthNecessary> monthNecessaryList = monthNecessaryService.getTabExist(id);
        if (monthNecessaryList != null && monthNecessaryList.size() > 0) {
            InfoProject infoProject = new InfoProject();
            infoProject.setId("0");
            infoProject.setNum("0");
            infoProject.setContent("必备条件");
            infoProject.setType("1");
            resultList.add(infoProject);
        }
        //查询动态标签
        List<InfoProject> infoProjects = infoProjectService.getAuditTabbyDepandTaskID(id);
        resultList.addAll(infoProjects);
        modelMap.put("tabTitleList", resultList);
        modelMap.put("taskId", id);
        return prefix + "/viewresult";
    }
}
